package com.carson.undergo.ui.activity.home

import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.view.View
import com.carson.undergo.R
import com.carson.undergo.databinding.UiElectronSignBinding
import com.carson.undergo.utils.ImageUtils
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import com.model.common.base.activity.BaseActivity
import com.model.common.utils.always.ToastUtils

/**
 * Create by Carson on 2022/6/15.
 * 白板签字
 */
class ElectronSignUI : BaseActivity<UiElectronSignBinding>() {

    override fun getViewBinding(): UiElectronSignBinding =
        UiElectronSignBinding.inflate(layoutInflater)

    override fun initViewsAndEvents() {
        binding.lineName.setBackColor(Color.WHITE)
        binding.lineName.setPaintWidth(20)
        binding.lineName.setPenColor(Color.BLACK)
        binding.lineName.clear()
    }

    override fun addListener() {
        binding.btnClear.setOnClickListener(this)
        binding.btnSave.setOnClickListener(this)
    }

    override fun onWidgetClick(view: View?) {
        when (view?.id) {
            //清除
            R.id.btn_clear -> {
                binding.lineName.clear()
                binding.lineName.setBackColor(Color.WHITE)
                binding.lineName.setPaintWidth(20)
                binding.lineName.setPenColor(Color.BLACK)
            }
            //保存
            R.id.btn_save -> {
                if (binding.lineName.touched) {
                    val bitmap = createBitMapForView(binding.lineName)
                    if (bitmap != null) {
                        saveImage(bitmap)
                    }
                } else {
                    ToastUtils.showMessage(mContext, "您还没有签名哦~")
                }
            }
        }
    }

    private fun saveImage(bitmap: Bitmap) {
        XXPermissions.with(mContext)
            .permission(Permission.READ_EXTERNAL_STORAGE, Permission.WRITE_EXTERNAL_STORAGE)
            .request(object : OnPermissionCallback {
                override fun onGranted(permissions: MutableList<String>?, all: Boolean) {
                    if (all) {
                        ImageUtils.saveImageToGallery(mContext, bitmap)
                        binding.lineName.destroyDrawingCache()
                    } else {
                        ToastUtils.showMessage(mContext, "没有读写权限无法保存图片哦~")
                    }
                }

                override fun onDenied(permissions: MutableList<String>?, never: Boolean) {
                    if (never) {
                        ToastUtils.showMessage(mContext, "被永久拒绝授权,请手动授予读写权限");
                        XXPermissions.startPermissionActivity(
                            this@ElectronSignUI,
                            permissions
                        )
                    } else {
                        ToastUtils.showMessage(mContext, "获取读写权限失败")
                    }
                }
            })
    }

    /**
     * 获取view生成bitmap
     */
    private fun createBitMapForView(view: View): Bitmap? {
        view.measure(
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
        )
        val width = view.width
        val height = view.height
        view.layout(0, 0, width, height)
        val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565)
        val canvas = Canvas(bitmap)
        view.draw(canvas)
        canvas.save()
        return bitmap
    }
}